<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:msbld="http://schemas.microsoft.com/developer/msbuild/2003" xmlns:nyext="urn:noury-extensions" exclude-result-prefixes="xsl nyext msxsl msbld">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />

  <xsl:param name="id" />
  <xsl:param name="trmdir" />

  <xsl:include href="functions.xslt" />

  <xsl:template match="reference" mode="extern">
    <Reference>
      <xsl:variable name="full" select="nyext:getAssemblyName(@name, nyext:combinePath(nyext:combinePath($trmdir, 'lib'), /manifest/configuration/platform[1]/@name))" />
      <xsl:attribute name="Include">
        <xsl:value-of select="$full" />
      </xsl:attribute>
      <xsl:if test="@name!=$full">
        <SpecificVersion>
          <xsl:text>False</xsl:text>
        </SpecificVersion>
        <HintPath>
          <xsl:apply-templates select="." mode="path-to-root">
            <xsl:with-param name="root" select="ancestor::solution[1]" />
          </xsl:apply-templates>
          <xsl:text>..\lib\</xsl:text>
          <xsl:value-of select="@name" />
          <xsl:text>.dll</xsl:text>
        </HintPath>
      </xsl:if>
    </Reference>
  </xsl:template>

  <xsl:template match="reference" mode="intern">
    <xsl:variable name="projectName" select="@project" />
    <xsl:variable name="project" select="ancestor::solution[1]//project[@name=$projectName]" />
    <xsl:variable name="ptr">
      <xsl:apply-templates select="." mode="path-to-root">
        <xsl:with-param name="root" select="ancestor::solution[1]" />
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="pfr">
      <xsl:apply-templates select="$project" mode="path-from-root">
        <xsl:with-param name="root" select="ancestor::solution[1]" />
      </xsl:apply-templates>
    </xsl:variable>
    <ProjectReference>
      <xsl:attribute name="Include">
        <xsl:value-of select="nyext:combinePath($ptr, $pfr)" />
      </xsl:attribute>
      <Project>
        <xsl:value-of select="$project/@id" />
      </Project>
      <Name>
        <xsl:value-of select="$project/@name" />
      </Name>
    </ProjectReference>
  </xsl:template>

  <xsl:template match="file">
    <xsl:variable name="file-name" select="@name" />
    <xsl:variable name="file-type" select="@type" />
    <xsl:if test="not(@type='res') and not(@type='datasource') and not(@type='bmp')">
      <Compile>
        <xsl:attribute name="Include">
          <xsl:apply-templates select="." mode="path-from-root">
            <xsl:with-param name="root" select="ancestor::project[1]" />
          </xsl:apply-templates>
          <xsl:value-of select="$file-name" />
          <xsl:text>.cs</xsl:text>
        </xsl:attribute>
      </Compile>
    </xsl:if>
    <xsl:for-each select="dependency">
      <xsl:if test="not(@type) or @type='cs'">
        <Compile>
          <xsl:attribute name="Include">
            <xsl:apply-templates select="." mode="path-from-root">
              <xsl:with-param name="root" select="ancestor::project[1]" />
            </xsl:apply-templates>
            <xsl:value-of select="@name" />
            <xsl:text>.cs</xsl:text>
          </xsl:attribute>
          <AutoGen>
            <xsl:text>True</xsl:text>
          </AutoGen>
          <xsl:if test="@design='true'">
            <DesignTime><xsl:text>True</xsl:text></DesignTime>
          </xsl:if>
          <DependentUpon>
            <xsl:value-of select="$file-name" />
            <xsl:choose>
              <xsl:when test="$file-type='res'">
                <xsl:text>.resx</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>.cs</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </DependentUpon>
        </Compile>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template match="file | dependency" mode="res">
    <xsl:variable name="file-name" select="@name" />
    <EmbeddedResource>
      <xsl:attribute name="Include">
        <xsl:apply-templates select="." mode="path-from-root">
          <xsl:with-param name="root" select="ancestor::project[1]" />
        </xsl:apply-templates>
        <xsl:value-of select="$file-name" />
        <xsl:text>.resx</xsl:text>
      </xsl:attribute>
      <SubType><xsl:text>Designer</xsl:text></SubType>
      <xsl:choose>
        <xsl:when test="local-name()='dependency'">
          <DependentUpon>
            <xsl:value-of select="../@name" />
            <xsl:text>.cs</xsl:text>
          </DependentUpon>
        </xsl:when>
        <xsl:otherwise>
          <Generator><xsl:text>ResXFileCodeGenerator</xsl:text></Generator>
          <LastGenOutput>
            <xsl:value-of select="dependency/@name" />
            <xsl:text>.cs</xsl:text>
          </LastGenOutput>
        </xsl:otherwise>
      </xsl:choose>
    </EmbeddedResource>
  </xsl:template>
  
  <xsl:template match="file" mode="bmp">
    <xsl:variable name="file-name" select="@name" />
    <EmbeddedResource>
      <xsl:attribute name="Include">
        <xsl:apply-templates select="." mode="path-from-root">
          <xsl:with-param name="root" select="ancestor::project[1]" />
        </xsl:apply-templates>
        <xsl:value-of select="$file-name" />
        <xsl:text>.bmp</xsl:text>
      </xsl:attribute>
    </EmbeddedResource>
  </xsl:template>
  
  <xsl:template match="file" mode="datasource">
    <xsl:variable name="file-name" select="@name" />
    <None>
      <xsl:attribute name="Include">
        <xsl:apply-templates select="." mode="path-from-root">
          <xsl:with-param name="root" select="ancestor::project[1]" />
        </xsl:apply-templates>
        <xsl:value-of select="$file-name" />
        <xsl:text>.datasource</xsl:text>
      </xsl:attribute>
    </None>
  </xsl:template>

  <xsl:template match="project">
    <Project DefaultTargets="Build" ToolsVersion="3.5">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">
          <xsl:text>Debug</xsl:text>
        </Configuration>
        <Platform Condition=" '$(Platform)' == '' ">
          <xsl:value-of select="/manifest/configuration/platform[1]/@name" />
        </Platform>
        <ProductVersion>
          <xsl:text>9.0.30729</xsl:text>
        </ProductVersion>
        <SchemaVersion>
          <xsl:text>2.0</xsl:text>
        </SchemaVersion>
        <ProjectGuid>
          <xsl:value-of select="@id" />
        </ProjectGuid>
        <OutputType>
          <xsl:text>Library</xsl:text>
        </OutputType>
        <AppDesignerFolder>
          <xsl:text>Properties</xsl:text>
        </AppDesignerFolder>
        <AssemblyName>
          <xsl:value-of select="@assembly" />
        </AssemblyName>
        <SignAssembly>
          <xsl:text>True</xsl:text>
        </SignAssembly>
        <AssemblyOriginatorKeyFile>
          <xsl:apply-templates select="." mode="path-to-root">
            <xsl:with-param name="root" select="ancestor::solution[1]" />
          </xsl:apply-templates>
          <xsl:text>misc\Project.snk</xsl:text>
        </AssemblyOriginatorKeyFile>
        <DelaySign>
          <xsl:text>False</xsl:text>
        </DelaySign>
        <AssemblyOriginatorKeyMode>
          <xsl:text>File</xsl:text>
        </AssemblyOriginatorKeyMode>
        <ReferencePath>
          <xsl:text>$(MSBuildProjectDirectory)\</xsl:text>
          <xsl:apply-templates select="." mode="path-to-root">
            <xsl:with-param name="root" select="ancestor::solution[1]" />
          </xsl:apply-templates>
          <xsl:text>..\lib\$(Platform)\</xsl:text></ReferencePath>
        <DocumentationFile>
          <xsl:text>bin\$(Platform)\$(Configuration)\$(AssemblyName).xml</xsl:text>
        </DocumentationFile>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Platform)' == '.NET 2.0' Or '$(Platform)' == '.NET CF 2.0' ">
        <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Platform)' == '.NET 3.0' ">
        <TargetFrameworkVersion>v3.0</TargetFrameworkVersion>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Platform)' == '.NET 3.5' Or '$(Platform)' == '.NET CF 3.5' ">
        <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
        <DebugSymbols>
          <xsl:text>true</xsl:text>
        </DebugSymbols>
        <DebugType>
          <xsl:text>full</xsl:text>
        </DebugType>
        <Optimize>
          <xsl:text>false</xsl:text>
        </Optimize>
        <DefineConstants>
          <xsl:text>DEBUG;TRACE</xsl:text>
        </DefineConstants>
        <ErrorReport>
          <xsl:text>prompt</xsl:text>
        </ErrorReport>
        <WarningLevel>
          <xsl:text>4</xsl:text>
        </WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
        <DebugType>
          <xsl:text>pdbonly</xsl:text>
        </DebugType>
        <Optimize>
          <xsl:text>true</xsl:text>
        </Optimize>
        <DefineConstants>
          <xsl:text>TRACE</xsl:text>
        </DefineConstants>
        <ErrorReport>
          <xsl:text>prompt</xsl:text>
        </ErrorReport>
        <WarningLevel>
          <xsl:text>4</xsl:text>
        </WarningLevel>
        <TreatWarningsAsErrors>
          <xsl:text>true</xsl:text>
        </TreatWarningsAsErrors>
        <NoWarn>
        </NoWarn>
      </PropertyGroup>
      <ItemGroup Condition=" '$(Platform)' == '.NET 3.0' Or '$(Platform)' == '.NET 3.5' ">
        <Reference Include="WindowsBase" />
      </ItemGroup>
      <ItemGroup Condition=" '$(Platform)' == '.NET 3.5' Or '$(Platform)' == '.NET CF 3.5' ">
        <Reference Include="System.Core">
          <RequiredTargetFramework>3.5</RequiredTargetFramework>
        </Reference>
      </ItemGroup>
      <xsl:for-each select="/manifest/configuration/platform[@mode='.NET CF']">
        <PropertyGroup>
          <xsl:attribute name="Condition">
            <xsl:text> '$(Configuration)|$(Platform)' == 'Release|</xsl:text>
            <xsl:value-of select="nyext:xmlEncode(@name)" />
            <xsl:text>' </xsl:text>
          </xsl:attribute>
          <NoWarn>444</NoWarn>
        </PropertyGroup>
      </xsl:for-each>
      <xsl:variable name="ref35" select="reference[string-length(@name) &gt; 0 and @condition='.NET 3.5']" />
      <xsl:if test="$ref35">
        <ItemGroup Condition=" '$(Platform)' == '.NET 3.5' ">
          <xsl:apply-templates select="$ref35" mode="extern">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="ref30" select="reference[string-length(@name) &gt; 0 and @condition='.NET 3.0']" />
      <xsl:if test="$ref30">
        <ItemGroup Condition=" '$(Platform)' == '.NET 3.0' Or '$(Platform)' == '.NET 3.5' ">
          <xsl:apply-templates select="$ref30" mode="extern">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="ref20" select="reference[string-length(@name) &gt; 0 and @condition='.NET 2.0']" />
      <xsl:if test="$ref20">
        <ItemGroup Condition=" '$(Platform)' == '.NET 2.0' Or '$(Platform)' == '.NET 3.0' Or '$(Platform)' == '.NET 3.5' ">
          <xsl:apply-templates select="$ref20" mode="extern">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="ref" select="reference[@name and string-length(@condition)=0]" />
      <xsl:if test="$ref">
        <ItemGroup>
          <xsl:apply-templates select="$ref" mode="extern">
            <xsl:sort select="@name" />
          </xsl:apply-templates>
        </ItemGroup>
      </xsl:if>
      <ItemGroup>
        <xsl:apply-templates select=".//file" />
        <Compile>
          <xsl:attribute name="Include">
            <xsl:apply-templates select="." mode="path-to-root">
              <xsl:with-param name="root" select="ancestor::solution[1]" />
            </xsl:apply-templates>
            <xsl:text>GlobalAssemblyInfo.cs</xsl:text>
          </xsl:attribute>
          <Link>
            <xsl:text>Properties\GlobalAssemblyInfo.cs</xsl:text>
          </Link>
        </Compile>
        <Compile>
          <xsl:attribute name="Include">
            <xsl:apply-templates select="." mode="path-to-root">
              <xsl:with-param name="root" select="ancestor::solution[1]" />
            </xsl:apply-templates>
            <xsl:text>Version.cs</xsl:text>
          </xsl:attribute>
          <Link>
            <xsl:text>Properties\Version.cs</xsl:text>
          </Link>
        </Compile>
      </ItemGroup>
      <xsl:variable name="res-files" select=".//file[@type='res'] | .//dependency[@type='res']" />
      <xsl:if test="count($res-files) &gt; 0">
        <ItemGroup>
          <xsl:apply-templates select="$res-files" mode="res"/>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="bmp-files" select=".//file[@type='bmp']" />
      <xsl:if test="count($bmp-files) &gt; 0">
        <ItemGroup>
          <xsl:apply-templates select="$bmp-files" mode="bmp"/>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="datasource-files" select=".//file[@type='datasource']" />
      <xsl:if test="count($datasource-files) &gt; 0">
        <ItemGroup>
          <xsl:apply-templates select="$datasource-files" mode="datasource"/>
        </ItemGroup>
      </xsl:if>
      <xsl:variable name="iref" select="reference[@project]" />
      <xsl:if test="$iref">
        <ItemGroup>
          <xsl:apply-templates select="$iref" mode="intern">
            <xsl:sort select="@project" />
          </xsl:apply-templates>
        </ItemGroup>
      </xsl:if>
      <xsl:comment>&lt;Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /&gt;</xsl:comment>
      <Import>
        <xsl:attribute name="Project">
          <xsl:apply-templates select="." mode="path-to-root">
            <xsl:with-param name="root" select="ancestor::solution[1]" />
          </xsl:apply-templates>
          <xsl:text>misc\Salamanca.CSharp.targets</xsl:text>
        </xsl:attribute>
      </Import>
    </Project>
  </xsl:template>

  <!-- Entry point for the transformation -->
  <xsl:template match="/">
    <xsl:apply-templates select="//project[@id=$id]" />
  </xsl:template>
</xsl:stylesheet>
